﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using XFControls.XFPanels.XFPanelItems;
using XFSenseUI.XFItems.SenseControls;

namespace XFItemControls.XFPanels.XFPanelItems
{
    public class XFItemTwoButtons : XFItemBase
    {
        public XFItemTwoButtons()
        {
            LeftButton = new SenseControlButton("");
            RightButton = new SenseControlButton("");

            Style = new XFItemStyle
                        {
                            BorderBottomColor = Color.FromArgb(189, 182, 189),
                            DashStyleBottom = DashStyle.Dash,
                            TextColor = Color.Black,
                            TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                            SelectedTextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                            SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                            SelectedSecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                            SecondaryTextColor = Color.FromArgb(57, 52, 57),
                            SelectedTextColor = Color.White,
                            SelectedBackgroundColor = Color.FromArgb(43, 36, 43),
                            SelectedSecondaryTextColor = Color.FromArgb(182, 178, 182),
                            Padding = 10
                        };

            ItemType = XFItemType.Clickable;
        }

        public SenseControlButton LeftButton { get; set; }
        public SenseControlButton RightButton { get; set; }


        public override int GetHeight()
        {
            return Math.Max(LeftButton.GetHeight(), RightButton.GetHeight()) + Style.PaddingTop + Style.PaddingBottom;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            int width = GetWidth();
            width -= RightButton.ItemRectangle.Width + Style.PaddingRight;

            LeftButton.ItemPaint(g, Style.PaddingLeft, Style.PaddingTop);
            RightButton.ItemPaint(g, width, Style.PaddingTop);
        }

        public override void OnMouseDown(int x, int y)
        {
            int width = GetWidth();
            width -= RightButton.ItemRectangle.Width + Style.PaddingRight;

            Rectangle recL = LeftButton.ItemRectangle;
            recL.X = Style.PaddingLeft;
            recL.Y = Style.PaddingTop;

            Rectangle recR = RightButton.ItemRectangle;
            recR.X = width;
            recR.Y = Style.PaddingTop;

            base.OnMouseDown(x, y);
            var p = new Point(x, y);

            if (recL.Contains(p))
                LeftButton.OnMouseDown(x - recL.X, y - recL.Y);

            if (recR.Contains(p))
                RightButton.OnMouseDown(x - recL.X, y - recL.Y);
        }

        public override void OnMouseMove(int x, int y)
        {
            base.OnMouseMove(x, y);
            int width = GetWidth();
            width -= RightButton.ItemRectangle.Width + Style.PaddingRight;

            Rectangle recL = LeftButton.ItemRectangle;
            recL.X = Style.PaddingLeft;
            recL.Y = Style.PaddingTop;

            Rectangle recR = RightButton.ItemRectangle;
            recL.X = width;
            recL.Y = Style.PaddingTop;

            var p = new Point(x, y);

            if (recL.Contains(p))
                LeftButton.OnMouseMove(x - recL.X, y - recL.Y);

            if (recR.Contains(p))
                RightButton.OnMouseMove(x - recL.X, y - recL.Y);
        }

        public override XFItemClickResult GetClickResult()
        {
            if (RightButton.State == XFItemState.Selected)
                return RightButton.GetClickResult();

            if (LeftButton.State == XFItemState.Selected)
                return LeftButton.GetClickResult();

            return base.GetClickResult();
        }
    }
}